package top.wardyan.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * <p>HTTP工具</p>
 * <p>2019-10-30</p>
 * @author harrydeep@wardyan.top
 */
public class HttpUtil {
	
	private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
	
	/**
	 * <p>get(url, true, true)的快捷调用</p>
	 * <p>2019-10-30</p>
	 * @param url
	 * @return
	 * @throws IOException 
	 */
	public static String get(String url) throws IOException {
		return get(url, true, true);
	}
	
	/**
	 * <p>以GET方式请求服务器</p>
	 * <p>2019-10-30</p>
	 * @param url 网址
	 * @param isOutput 是否有输出内容. 有输出内容才会返回string
	 * @param isInput 是否有输入参数
	 * @return 网址返回的内容的文本
	 * @throws Exception 
	 */
	public static String get(String url, Boolean isOutput, Boolean isInput) throws IOException {
		URL u = null;
		HttpURLConnection con = null;
		StringBuffer buffer = new StringBuffer(); 
		try {  
            u = new URL(url);  
            con = (HttpURLConnection) u.openConnection();  
            // POST 只能为大写，严格限制，post会不识别  
            con.setRequestMethod("GET");  
            con.setDoOutput(isOutput);  
            con.setDoInput(isInput);  
            con.setUseCaches(false);  
            //con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
            //OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");  
            //osw.write(sb.toString());  
            //osw.flush();  
            //osw.close();  
            // 读取返回内容  
             
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));  
            String temp;
            while ((temp = br.readLine()) != null) {  
                buffer.append(temp);  
                buffer.append("\n");  
            }
            if(buffer.length() > 0 && buffer.charAt(buffer.length() - 1) == '\n') {
            	buffer.deleteCharAt(buffer.length() - 1);
            }
        } catch (IOException e) {  
            throw e;
        } finally {
            if (con != null) {  
                con.disconnect();  
            }  
        }  
		return isOutput ? buffer.toString() : "";
	}
	
	/**
	 * <p>以POST方式请求服务器</p>
	 * <p>2019-10-30</p>
	 * @param url 地址
	 * @param data 参数内容.注意,格式是:rCustID=?&sID=?&dID=?这样的
	 * @param isOutput 是否有输出内容.如果有的话才会返回文本,否则只会返回0长度文本
	 * @return 返回的内容
	 */
	public static String post(String url, String data, Boolean isOutput) throws IOException {
		URL u = null;
		HttpURLConnection con = null;
		StringBuffer buffer = new StringBuffer();  
		try {  
            u = new URL(url);  
            con = (HttpURLConnection) u.openConnection();  
            //// POST 只能为大写，严格限制，post会不识别  
            con.setRequestMethod("POST");  
            con.setDoOutput(isOutput);  
            con.setDoInput(true);  
            con.setUseCaches(false);  
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
            OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
            osw.write(data);  
            osw.flush();  
            osw.close();  
          //一定要有返回值，否则无法把请求发送给server端。  
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));  
            String temp;  
            while ((temp = br.readLine()) != null) {  
                buffer.append(temp);  
                buffer.append("\n");  
            }  
            
        } catch (IOException e) {  
            throw e;
        } finally {  
            if (con != null) {  
                con.disconnect();  
            }  
        }
		return isOutput ? buffer.toString() : "";
	}

	public static String post(String url, String data) throws IOException {
		return post(url, data, true);
	}

}
